//
//  HOReportOrderEntity.m
//  微度报修
//
//  Created by foguo-mac-1 on 6/22/16.
//  Copyright © 2016 WeiDu. All rights reserved.
//

#import "HOReportOrderEntity.h"
#import "HOConstants.h"
#import "HOStatusEntity.h"
#import "HOUtils.h"


@implementation HOReportOrderEntity

- (instancetype)init
{
	self = [super init];
	if(self)
	{
		self.statusList = [[[NSMutableArray alloc] init] autorelease];
		statusEntityDic = [[NSMutableDictionary alloc] init];
		
	}
	return self;
}

- (void)parseDic:(NSDictionary *)dic
{
	self.orderId = [[dic objectForKey:kHO_FIELD_OrderId] intValue];
	self.orderSN = [[dic objectForKey:kHO_FIELD_OrderSn] intValue];
	self.createTime = [[dic objectForKey:kHO_FIELD_CreateTime] doubleValue];
	self.address = [HOUtils getValideString:[dic objectForKey:kHO_FIELD_FaultAddress]];
	self.detail = [HOUtils getValideString:[dic objectForKey:kHO_FIELD_FaultDetail]];
	self.imageList = [dic objectForKey:kHO_FIELD_FaultImage];
	self.image = [dic objectForKey:kHO_FIELD_FaultImage];
	
	self.voice = [dic objectForKey:kHO_FIELD_FaultVoice];
	self.doneId = [[dic objectForKey:@"DoneId"] intValue];
	self.isAssistantReport = [[dic objectForKey:kHO_FIELD_AssistantData] boolValue];
	self.isUrgent = [[HOUtils getValideString:[dic objectForKey:kHO_FIELD_Isurgent]] boolValue];
	
	self.isResubmit = [[dic objectForKey:kHO_FIELD_isReSubmit_low] intValue];
	self.repairmanDepName = [dic objectForKey:kHO_FIELD_RepairmanDepName];
	self.repairmanHeadpic = [dic objectForKey:kHO_FIELD_RepairmanHeadpic];
	self.repairmanId = [[dic objectForKey:kHO_FIELD_RepairmanId] intValue];
	self.repairmanInfo = [dic objectForKey:kHO_FIELD_RepairmanInfo];
	self.repairmanMobile = [dic objectForKey:kHO_FIELD_RepairmanMobile];
	self.repairmanName = [dic objectForKey:kHO_FIELD_RepairmanName];
	self.postDepartmentName = [dic objectForKey:kHO_FIELD_PostDepartmentName];
	self.postDepartmentId = [[dic objectForKey:kHO_FIELD_PostDid] intValue];
	
	
	self.theNumberDay = [dic objectForKey:@"kTheDayNumber"];
	
	self.status = [[dic objectForKey:kHO_FIELD_Status] intValue];
	self.isUncompleted = [[HOUtils getValideString:[dic objectForKey:kHO_FIELD_IsIncomplete]] boolValue];
	if(self.status == HO_REPORT_ORDER_ACCEPTED && self.isUncompleted)
	{
		self.status = HO_REPORT_ORDER_UNCOMPLETED;
	}
	self.postId = [[dic objectForKey:kHO_FIELD_PostId] intValue];
	self.appraise = [[dic objectForKey:kHO_FIELD_Score] floatValue];
	NSArray *array = [dic objectForKey:kHO_FIELD_RepairmanInfo];
	[self.statusList removeAllObjects];
	for(int i = 0; i < [array count]; i ++)
	{
		NSDictionary *statusDic = [array objectAtIndex:i];
		int status = [[dic objectForKey:kHO_FIELD_Status] intValue];
		HOStatusEntity *entity = [statusEntityDic objectForKey:[NSNumber numberWithInt:status]];
		if(entity == nil)
		{
			entity = [[[HOStatusEntity alloc] init] autorelease];
			[statusEntityDic setObject:entity forKey:[NSNumber numberWithInt:status]];
		}
		
		[entity parseDic:statusDic];
		[self.statusList addObject:entity];
		
		if(entity.status == HO_REPORT_ORDER_ACCEPTED && self.isUncompleted)
		{
			HOStatusEntity *uncompleteEentity = [[[HOStatusEntity alloc] init] autorelease];
			[statusEntityDic setObject:uncompleteEentity forKey:[NSNumber numberWithInt:HO_REPORT_ORDER_UNCOMPLETED]];
			[entity parseDic:statusDic];
			entity.status = HO_REPORT_ORDER_UNCOMPLETED;
			[self.statusList addObject:entity];
		}
	}
	
}
- (void)dealloc
{
	[self.statusList removeAllObjects];
	self.statusList = nil;

	self.orderSN = 0;
	self.createTime = 0;
	self.address = nil;
	self.detail = nil;
	self.image = nil;
	self.voice = nil;
	[statusEntityDic release];
	statusEntityDic = nil;
	[super dealloc];
}

- (NSString *)getFaultDetailContent
{
	NSString *content = [HOUtils getValideString:self.detail];
	if([self.detail isEqualToString:@"无"])
	{
		BOOL isPictureDetail = (self.image && self.image.length > 0 );
		BOOL isAudioDetail = (self.voice && self.voice.length > 0);
		if(isPictureDetail && isAudioDetail)
		{
			content = [NSString stringWithFormat:@"%@", @"[图片] + [语音]"];
		}
		else if(isPictureDetail)
		{
			content = [NSString stringWithFormat:@"%@", @"[图片]"];
		}
		else if(isAudioDetail)
		{
			content = [NSString stringWithFormat:@"%@", @"[语音]"];
		}
	}
	return content;
}


- (NSString *)getStatusStr
{
	NSString *content = [HOUtils getDescribtionWithStatus:self.status];
	if(self.isUncompleted)
	{
		content = @"未完成";
	}
	return content;
}
@end
